  protected function getFirstItemFromCart()
  {
    if (count($items = $this->configuration->getCartItems()) > 0)
    {
      return array_shift($items);
    }

    return null;
  }

  protected function getFirstItemFromCartAsObject($condition = null)
  {
    if (count($items = $this->configuration->getCartItemsAsObjects($condition)) > 0)
    {
      return array_shift($items);
    }

    return null;
  }
  
  public function executeCartBatchFormAction(sfWebRequest $request)
  {
    $this->formClass = $request->getParameter('form_class');
    $condition       = $request->getParameter('batch_c');
    $this-><?php echo $this->getSingularName() ?> = $this->getFirstItemFromCartAsObject($condition);

    if ($this->getUser()->hasFlash('error'))
    {
      $this->getUser()->setFlash('error', $this->getUser()->getFlash('error'));
    }

    if ($this->getUser()->hasFlash('notice'))
    {
      $this->getUser()->setFlash('notice', $this->getUser()->getFlash('notice'));
    }

    if (is_null($this-><?php echo $this->getSingularName() ?>))
    {
      $this->getUser()->setFlash('error', 'No eligible items in cart.');
      $this->redirect('@<?php echo $this->getUrlForAction('list') ?>');
    }

    $this->cart_objects = $this->configuration->getCartItemsAsObjects($condition);
    $this->formClass    = $request->getParameter('form_class', null);
    $this->form         = new $this->formClass($this-><?php echo $this->getSingularName() ?>, array('cartObjects' => $this->cart_objects));
  }

  public function executeCartBatchFormActionUpdate(sfWebRequest $request)
  {
    $condition = $request->getParameter('batch_c');
    $items     = $this->configuration->getCartItemsAsObjects($condition);

    if (count($items) == 0)
    {
      $this->getUser()->setFlash('error', 'No eligible items in cart.');
      $this->redirect('@<?php echo $this->getUrlForAction('list') ?>');
    }
    else
    {
      $this->formClass = $request->getParameter('form_class');
      $this->cartBatchUpdateProcessForm($request, $this->formClass, $items);
    }

    $this->cart_objects = $this->configuration->getCartItemsAsObjects($condition);
    $this->setTemplate('cartBatchFormAction');
  }

  protected function correctBatchFormRequest($requestParamName, $object)
  {
    $requestParam = sfContext::getInstance()->getRequest()->getParameter($requestParamName, array());
    if (isset($requestParam['id']))
    {
      $requestParam['id'] = $object->getId();
      sfContext::getInstance()->getRequest()->setParameter($requestParamName, $requestParam);
    }
    return $requestParam;
  }

  public function cartBatchUpdateProcessForm($request, $formClass, $items)
  {
    $this->getContext()->getConfiguration()->loadHelpers(array('I18N'));
    $ok_msjs = array();
    $er_msjs = array();
    $redirect = true;
    $processed = array();

    foreach ($items as $object)
    {
      $form   = new $formClass($object);
      $method = (method_exists($this, 'correctBatch'.get_class($form).'Request'))? 'correctBatch'.get_class($form).'Request' : 'correctBatchFormRequest';
      $params = $this->$method($form->getName(), $object);
      $form->bind($params, $request->getFiles($form->getName()));
      if ($form->isValid())
      {
        try {
          $object = $form->save();
          $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $object)));
          $ok_msjs[] = __("Inventory %%number%% successfully saved", array('%%number%%' => $object->getInventoryNumber()));
          $processed[] = $object->getPrimaryKey();
        } catch (Exception $e) {
          $er_msjs[] = __("Inventory %%number%% could not be saved due to a database error", array('%%number%%' => $object->getInventoryNumber()));
        }
      }
      else
      {
        $er_msjs[] = __("Inventory %%number%% could not be saved due to an input error", array('%%number%%' => $object->getInventoryNumber()));
        $redirect = false;
        $this-><?php echo $this->getSingularName() ?> = $object;
        $this->form = $form;
        break;
      }
    }

    if (count($ok_msjs) > 0)
    {
      $this->getUser()->setFlash('notice', 'The following items has been updated');
      $this->getUser()->setFlash('batch_executed', $ok_msjs);
    }

    if (count($er_msjs) > 0)
    {
      $this->getUser()->setFlash('error', 'The following items could not be updated');
      $this->getUser()->setFlash('batch_not_executed', $er_msjs);
    }

    if (count($processed) > 0 && !$redirect)
    {
      $this->configuration->setCartItems(array_diff($this->configuration->getCartItems(), $processed));
    }

    if ($redirect)
    {
      $this->redirect('<?php echo $this->getModuleName()?>/cartIndex');
    }
  }

